package com.tourism.website.login.service.impl;

import com.tourism.website.login.entity.User;
import com.tourism.website.login.mapper.UserMapper;
import com.tourism.website.login.utils.JwtUtil;
import com.tourism.website.login.utils.Md5Util;
import com.tourism.website.login.service.UserService;
import com.tourismwebsite.login.query.UserQuery;
import com.tourismwebsite.vo.JsonVO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserService userService;
    @Override
    public User findByUsername(String username) {
        User u = userMapper.findByUserName(username);
        return u;
    }

    @Override
    public Integer findRole(String username) {
        Integer u = userMapper.findRole(username);
        return u;
    }

    @Override
    public boolean insertRoleId(int roleId,String username,Integer userId) {
        boolean role = userMapper.insertRoleId(roleId,username,userId);
        return role;
    }

    @Override
    public String QueryUser(UserQuery userQuery) {
        //根据用户名查询用户
        User loginUser = findByUsername(userQuery.getUsername());
        Integer role = findRole(userQuery.getUsername());
        //判断用户是否存在
        if(loginUser==null){
            return "用户名不存在";
        }
        if(role == null){
            return "角色信息不存在";
        }

        //10为管理员，1为普通用户
        if(role !=null &&role == 1){
            if(Md5Util.getMD5String(userQuery.getPassword()).equals(loginUser.getPassword())){
                Map<String,Object> claims = new HashMap<>();
                claims.put("id",loginUser.getUserId());
                claims.put("username",loginUser.getUser_name());
                String token = JwtUtil.genToken(claims);
                return role +"拼接"+token;
            }
        }else{
            if(Md5Util.getMD5String(userQuery.getPassword()).equals(loginUser.getPassword())){
                Map<String,Object> claims = new HashMap<>();
                claims.put("id",loginUser.getUserId());
                claims.put("username",loginUser.getUser_name());
                String token = JwtUtil.genToken(claims);
                return role +"拼接"+token;
            }
        }
        //判断密码是否正确
        return "用户名或者密码错误";
    }
    @Override
    public Boolean InsertUser(UserQuery userQuery) {
        User u = userService.findByUsername(userQuery.getUsername());
        if(u == null) {
            // 加密密码
            String md5String = Md5Util.getMD5String(userQuery.getPassword());
            // 添加用户
            int result = userMapper.register(userQuery.getUsername(), userQuery.getNickname(), md5String,userQuery.getAvatar());
            if (result > 0) {
                // 获取插入用户的ID
                User newUser = userService.findByUsername(userQuery.getUsername());
                if (newUser != null) {
                    insertRoleId(1, userQuery.getUsername(), newUser.getUserId());
                    return true;
                }
            }
        }
        return false;
    }

}
